package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.IsStatus;
import com.prd.mapper.EntGoalCoachMapper;
import com.prd.model.EntGoalCoach;
import com.prd.model.LoginUser;
import com.prd.service.EntGoalCoachService;
import com.prd.service.UserService;
import com.prd.util.StringsUtils;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntGoalCoachServiceImpl extends ServiceImpl<EntGoalCoachMapper, EntGoalCoach> implements EntGoalCoachService {

    @Autowired
    private EntGoalCoachMapper entGoalCoachMapper;

    @Autowired
    private UserService userService;

    @Override
    public int insertInfo(EntGoalCoach entGoalCoach) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entGoalCoach);
        entGoalCoach.setVerify(IsStatus.TWO.getCode());
        return entGoalCoachMapper.insert(entGoalCoach);
    }

    @Override
    public int updateInfo(EntGoalCoach entGoalCoach) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entGoalCoach);
        return entGoalCoachMapper.updateById(entGoalCoach);
    }

    @Override
    public List<EntGoalCoach> listInfo(EntGoalCoach entGoalCoach) {
        LambdaQueryWrapper<EntGoalCoach> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(entGoalCoach);
        List<EntGoalCoach> list = entGoalCoachMapper.selectList(queryWrapper);
        for (EntGoalCoach entGoalCoach1 : list) {
            entGoalCoach1.setSigner(userService.listName(Long.parseLong(entGoalCoach1.getSigner())).getMerge());
            entGoalCoach1.setRecipient(userService.listName(Long.parseLong(entGoalCoach1.getRecipient())).getMerge());
            entGoalCoach1.setCompile(userService.listName(Long.parseLong(entGoalCoach1.getCompile())).getMerge());
            entGoalCoach1.setMake(userService.listName(Long.parseLong(entGoalCoach1.getMake())).getMerge());
        }
        return list;
    }

    @Override
    public EntGoalCoach details(Long id) {
        return entGoalCoachMapper.selectById(id);
    }
}
